package com.cxkj.device_serial;

import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class DeviceSerialPlugin extends UniModule {

    private static final String TAG = "DeviceSerialPlugin";

    @UniJSMethod(uiThread = false)
    public void getDeviceSerial(UniJSCallback callback) {
        try {
            String serial = Build.SERIAL;
            if (serial == null || serial.equalsIgnoreCase("unknown")) {
                serial = Settings.Secure.getString(mUniSDKInstance.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
            }

            JSONObject result = new JSONObject();
            result.put("serial", serial);
            callback.invoke(result);
        } catch (Exception e) {
            Log.e(TAG, "获取序列号失败", e);
            JSONObject error = new JSONObject();
            error.put("error", "无法获取设备序列号");
            callback.invoke(error);
        }
    }
}